/*  KKS - KinoKassenSystem 2013
 * 
 *  @author Sebastian Schöppner
 * 
 *  seatsview of the specific movie
 * 
 *  
 */
package kks;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JPanel;

public class PanelSeats extends JPanel
{
    
    private PanelNumpad pnlNumpad;
    private int SelectedCount = 0;
    private cMovie Movie;
    
    ArrayList<SeatSquare> ListSeats;
    
    @Override
    public void paintComponent(Graphics g) 
    {
           super.paintComponent(g);
           clearSeats();
           addSeats();
           paintSeats(g);           
    }
       
    /* 
     * clear the list of seats
     */
    public void clearSeats()
    {
        ListSeats.clear();
    }
    
    /**
    * pant the seats onto the graphic object
    * <p>
    *
    * @param graphic object
    */ 
    private void paintSeats(Graphics g)
    {                         
         //System.out.println(getWidth()/2);
         g.setColor(CustomColors.Highlight);
         g.fillRect(getWidth()/2 - 250, 0, 500, 50);
         
         //get the 
         for (int i = 0; i < ListSeats.size(); i++)
         {
             ListSeats.get(i).paintSquare(g);
         }

    }
    
    public void setNumpad(PanelNumpad numpad)
    {
        pnlNumpad = numpad;
    }
    
    public void addSeats()
    {
         int sizeSeats = 30;
         int startPosY = 70;
         int startPosX = getWidth()/2 - ((Movie.getSeats()*sizeSeats)/2);

         for (int x = startPosX; x < startPosX+Movie.getSeats()*sizeSeats; x+= sizeSeats+5)
            for (int y = startPosY; y < startPosY+Movie.getRows()*sizeSeats; y+= sizeSeats+5)               
            {
                SeatSquare seatSquare = new SeatSquare(x, y, 0);              
                ListSeats.add(seatSquare);                
            }         
    }
    
    public int getSelectedCount()
    {
        return SelectedCount;
    }
    
    public void addSelectedCount()
    {
        SelectedCount++;
    }
    
    public void delSelectedCount()
    {
        if (SelectedCount > 0)
            SelectedCount--;
    }
            
    public PanelSeats(cMovie movie, PanelNumpad numpad)
    {
        ListSeats = new ArrayList<>();
        Movie = movie;
        pnlNumpad = numpad;
        
        addMouseListener(new MouseAdapter()         
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                for (int i = 0; i < ListSeats.size(); i++)
                {
                    int posX = ListSeats.get(i).getX();
                    int posY = ListSeats.get(i).getY();

                    if (posX <= e.getX() &&  posX + ListSeats.get(i).getWidth() >= e.getX() &&
                            posY <= e.getY() &&  posY + ListSeats.get(i).getHeight() >= e.getY())
                    {
                        if (ListSeats.get(i).getSelected() == 0)
                        {
                            ListSeats.get(i).setSelected(1);
                            addSelectedCount();
                            //pnlNumpad.addItemToList(Movie.getMovieName(), getSelectedCount(), Movie.getPrice());                           
                            pnlNumpad.addItemToList(Movie, getSelectedCount());
                        }
                        else
                        {
                            ListSeats.get(i).setSelected(0);
                            delSelectedCount();
                            //pnlNumpad.addItemToList(Movie.getMovieName(), getSelectedCount(), Movie.getPrice());
                            pnlNumpad.addItemToList(Movie, getSelectedCount());
                            
                            
                        }
                        
                        ListSeats.get(i).paintSquare(getGraphics());
                    }
                        
                }     
            }
         });                
    }
   
    
    class SeatSquare 
    {
        private int xPos = 0;
        private int yPos = 0;
        private int width = 30;
        private int height = 30;
        private int Selected = 0;
        
        
        public SeatSquare()
        {

        }
        
        public SeatSquare(int seats, int rows, int selected)
        {
            xPos = seats;
            yPos = rows;
            Selected = selected;
        }
        
        public void setX(int xPos)
        { 
            this.xPos = xPos;
        }

        public int getX()
        {
            return xPos;
        }

        public void setY(int yPos)
        {
            this.yPos = yPos;
        }

        public int getY()
        {
            return yPos;
        }

        public int getWidth()
        {
            return width;
        } 

        public int getHeight()
        {
            return height;
        }

        public int setSelected(int selected)
        {
            Selected = selected;            
            return 1;
        }
        
        public int getSelected()
        {
            return Selected;            
        }
        
        public void paintSquare(Graphics g)
        {
            if (Selected == 0)
                g.setColor(CustomColors.Background);
            else
                g.setColor(CustomColors.MainColor);
            
            g.fillRect(xPos,yPos,width,height);
            g.setColor(CustomColors.Highlight);
            g.drawRect(xPos,yPos,width,height);  
        }        
    }
}
